<?php
include_once('inc/functions.inc.php');

if(!isEmpty(getParam('operation'))){
    
    switch(getParam('operation')) {
        case 'filtro': {
            fntAcademicoAsignacionCatedraticosFiltro($db);
            break;
        }
        case 'contenido': {
            fntAcademicoAsignacionCatedraticosContenido($db);
            break;
        }
        case 'secciones': {
            fntAcademicoAsignacionCatedraticosContenidoSecciones($db);
            break;
        }
        case 'materias': {
            fntAcademicoAsignacionCatedraticosContenidoMaterias($db);
            break;
        }
        case 'reporte': {
            fntAcademicoAsignacionCatedraticosReporte($db);
            break;
        }
        case 's': {
            fntAcademicoAsignacionCatedraticosGrabar($db);
            break;
        }
        case 'cuentas': {
            fntAcademicoAsignacionCatedraticosContabilidadGetCuentasUltimoNivel($db);
            break;
        }
    }
    die();
}

function fntAcademicoAsignacionCatedraticosFiltro($db){
    //VARIBLES
    $strId = "";
    $strQuery = "";
    $arrInfo = array();
    $strCodigo = "";
    $strNombre = "";
    $strSelected = "";
    //VARIBLES
    
    ?>
    <script type="text/javascript">
    var intFilaPadres = 0;
    </script>
    <br>
    <br>
    <div style="width: 100px; height: 30px; float: left;"><b>Catedratico</b></div>
    <div style="width: 900px; height: 30px; float: left;">
        <select name="txtAcademicoAsignacionCatedraticos_FK-CATEDRATICO" id="txtAcademicoAsignacionCatedraticos_FK-CATEDRATICO">
            <option value=""></option>
            <?php
            $strQuery = "SELECT * FROM cllg_teacher WHERE STATUS = '1'";
            $qTMP = $db->query($strQuery);
            while ( $rTMP = $db->fetch_array($qTMP) ) { 
                ?>
                <option value="<?php print $rTMP["ID"]; ?>"><?php print $rTMP["ID"]." - ".htmlentities($rTMP["FIRST_NAME"]." ".$rTMP["LAST_NAME"]); ?></option>
                <?php        
            }
            $db->free_result($qTMP);
            ?>
        </select>
    </div>
    <style>
    .ui-button { margin-left: -1px; }
    .ui-button-icon-only .ui-button-text { padding: 0.01em; } 
    .ui-autocomplete-input { margin: 0; padding: 0.0em 0 0.0em 0.0em; }
    </style>
    <script language="javascript" type="text/javascript">
    (function( $ ) {
        $.widget( "ui.combobox", {
            _create: function() {
                var self = this,
                    select = this.element.hide(),
                    selected = select.children( ":selected" ),
                    value = selected.val() ? selected.text() : "";
                var input = this.input = $( "<input>" )
                    .insertAfter( select )
                    .val( value )
                    .css("width", 300)
                    .autocomplete({
                        delay: 0,
                        minLength: 0,
                        source: function( request, response ) {
                            var matcher = new RegExp( $.ui.autocomplete.escapeRegex(request.term), "i" );
                            response( select.children( "option" ).map(function() {
                                var text = $( this ).text();
                                if ( this.value && ( !request.term || matcher.test(text) ) )
                                    return {
                                        label: text.replace(
                                            new RegExp(
                                                "(?![^&;]+;)(?!<[^<>]*)(" +
                                                $.ui.autocomplete.escapeRegex(request.term) +
                                                ")(?![^<>]*>)(?![^&;]+;)", "gi"
                                            ), "<strong>$1</strong>" ),
                                        value: text,
                                        option: this
                                    };
                            }) );
                        },
                        select: function( event, ui ) {  
                            ui.item.option.selected = true;
                            self._trigger( "selected", event, {
                                item: ui.item.option
                            });
                            $("[name='hdnAcademicoAsignacionCatedraticos_Id']").val("");
                            fntAcademicoAsignacionCatedraticosContenido();
                            fntAcademicoAsignacionCatedraticosReporte();
                        },
                        change: function( event, ui ) {
                            if ( !ui.item ) {
                                var matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( $(this).val() ) + "$", "i" ),
                                    valid = false;
                                select.children( "option" ).each(function() {
                                    if ( $( this ).text().match( matcher ) ) {
                                        this.selected = valid = true;
                                        return false;
                                    }
                                });
                                if ( !valid ) {
                                    // remove invalid value, as it didn't match anything
                                    $( this ).val( "" );
                                    select.val( "" );
                                    input.data( "autocomplete" ).term = "";
                                    return false;
                                }
                            }
                        }
                    })
                    .addClass( "ui-widget ui-widget-content ui-corner-left" );

                input.data( "autocomplete" )._renderItem = function( ul, item ) {
                    return $( "<li></li>" )
                        .data( "item.autocomplete", item )
                        .append( "<a>" + item.label + "</a>" )
                        .appendTo( ul );
                };

                this.button = $( "<button type='button'>&nbsp;</button>" )
                    .attr( "tabIndex", -1 )
                    .attr( "title", "Show All Items" )
                    .insertAfter( input )
                    .button({
                        icons: {
                            primary: "ui-icon-triangle-1-s"
                        },
                        text: false
                    })
                    .removeClass( "ui-corner-all" )
                    .addClass( "ui-corner-right ui-button-icon" )
                    .click(function() {
                        // close if already visible
                        if ( input.autocomplete( "widget" ).is( ":visible" ) ) {
                            input.autocomplete( "close" );
                            return;
                        }

                        // work around a bug (likely same cause as #5265)
                        $( this ).blur();

                        // pass empty string as value to search for, displaying all results
                        input.autocomplete( "search", "" );
                        input.focus();
                    });
            },

            destroy: function() {
                this.input.remove();
                this.button.remove();
                this.element.show();
                $.Widget.prototype.destroy.call( this );
            }
        });
    })( jQuery );

    $(function() {
        $("#txtAcademicoAsignacionCatedraticos_FK-CATEDRATICO").combobox();
    });
    </script>
    <script type="text/javascript">
    $(function() {
        $("[name='txtAcademicoAsignacionCatedraticos_FK-CATEDRATICO']").change( function(){
            $("[name='hdnAcademicoAsignacionCatedraticos_Id']").val("");
            fntAcademicoAsignacionCatedraticosContenido();
            fntAcademicoAsignacionCatedraticosReporte();
        });
    });
    </script>
    <?php
    
}
function fntAcademicoAsignacionCatedraticosContenido($db){
    //VARIBLES
    $strId = "";
    $strQuery = "";
    $arrInfo = array();
    $strCodigo = "";
    $strNombre = "";
    $strSelected = "";
    //VARIBLES
    
    $strId = isset($_POST["hdnAcademicoAsignacionCatedraticos_Id"]) ? $_POST["hdnAcademicoAsignacionCatedraticos_Id"] : "";
    $strCatedratico = isset($_POST["txtAcademicoAsignacionCatedraticos_FK-CATEDRATICO"]) ? $_POST["txtAcademicoAsignacionCatedraticos_FK-CATEDRATICO"] : "";
    if( !empty($strId) ) {
        $strQuery ="SELECT  FK_CATEDRATICO, FK_PERIODO, FK_GRADO, FK_SECCION, DATE_FORMAT(FECHA_ASIGNACION, '%d-%m-%Y') AS FECHA_ASIGNACION, DATE_FORMAT(FECHA_EXPIRACION, '%d-%m-%Y') AS FECHA_EXPIRACION 
                    FROM    cllg_academico_catedraticos_asignacion 
                    WHERE   ID = '{$strId}'";
        $qTMP = $db->query($strQuery);
        while ( $rTMP = $db->fetch_array($qTMP) ) { 
            $arrInfo = $rTMP;        
        }
        $db->free_result($qTMP);
    }
    
    $strCatedratico = isset($arrInfo["FK_CATEDRATICO"]) ? $arrInfo["FK_CATEDRATICO"] : "";
    $strPeriodo = isset($arrInfo["FK_PERIODO"]) ? $arrInfo["FK_PERIODO"] : "";
    $strGrado = isset($arrInfo["FK_GRADO"]) ? $arrInfo["FK_GRADO"] : "";
    $strSeccion = isset($arrInfo["FK_SECCION"]) ? $arrInfo["FK_SECCION"] : "";
    $strFechaAsignacion = isset($arrInfo["FECHA_ASIGNACION"]) ? $arrInfo["FECHA_ASIGNACION"] : "";
    $strFechaExpiracion = isset($arrInfo["FECHA_EXPIRACION"]) ? $arrInfo["FECHA_EXPIRACION"] : "";
    
    ?>
    <script type="text/javascript">
    var intFilaPadres = 0;
    </script>
    <br>
    <br>
    <div style="width: 100px; height: 30px; float: left;"><b>Periodo</b></div>
    <div style="width: 900px; height: 30px; float: left;">
        <select name="txtAcademicoAsignacionCatedraticos_fk-periodo" style="width: 200px;">
            <option value=""></option>
            <?php
            $strQuery = "SELECT * FROM cllg_academico_periodos WHERE ACTIVO = 'Y'";
            $qTMP = $db->queryToArray($strQuery);
            foreach ( $qTMP as $rTMP   ) { 
                $strSelected = ( $strPeriodo==$rTMP["ID"] ) ? "selected" : "";
                ?>
                <option value="<?php print $rTMP["ID"]; ?>" <?php print $strSelected; ?>><?php print $rTMP["NOMBRE"]; ?></option>
                <?php        
            }
            $db->free_result($qTMP);
            ?>
        </select>
    </div>
    <div style="width: 100px; height: 30px; float: left;"><b>Grado</b></div>
    <div style="width: 900px; height: 30px; float: left;">
        <select name="txtAcademicoAsignacionCatedraticos_fk-grado" style="width: 200px;">
            <option value=""></option>
            <?php
            $strQuery = "SELECT * FROM cllg_academico_grados WHERE ACTIVO = 'Y'";
            $qTMP = $db->query($strQuery);
            while ( $rTMP = $db->fetch_array($qTMP) ) { 
                $strSelected = ( $strGrado==$rTMP["ID"] ) ? "selected" : "";
                ?>
                <option value="<?php print $rTMP["ID"]; ?>" <?php print $strSelected; ?>><?php print $rTMP["CODIGO"]; ?> - <?php print $rTMP["NOMBRE"]; ?></option>
                <?php        
            }
            $db->free_result($qTMP);
            ?>
        </select>
    </div>
    <div style="width: 100px; height: 30px; float: left;"><b>Sección</b></div>
    <div style="width: 900px; height: 30px; float: left;" id="divAcademicoAsignacionCatedraticosContenidoSeccion">
        <select name="txtAcademicoAsignacionCatedraticos_fk-seccion">
            <option value="0"></option>
            <?php
            $strQuery ="SELECT  * 
                        FROM    cllg_academico_secciones,
                                cllg_academico_grados_secciones 
                        WHERE   cllg_academico_secciones.ID = cllg_academico_grados_secciones.FK_SECCION
                        AND     cllg_academico_grados_secciones.FK_GRADO = '{$strGrado}'";
            $qTMP = $db->query($strQuery);
            while ( $rTMP = $db->fetch_array($qTMP) ) { 
                $strSelected = ( $strSeccion==$rTMP["ID"] ) ? "selected" : "";
                ?>
                <option value="<?php print $rTMP["ID"]; ?>" <?php print $strSelected; ?> ><?php print $rTMP["NOMBRE"]; ?></option>
                <?php        
            }
            $db->free_result($qTMP);
            ?>
        </select>        
    </div>
    <div style="width: 100px; height: 30px; float: left;"><b>Asignatura</b></div>
    <div style="width: 900px; height: 30px; float: left;" id="divAcademicoAsignacionCatedraticosContenidoMateria">
        
    </div>
    <div style="width: 100px; height: 30px; float: left;"><b>Fecha Asignación</b></div>
    <div style="width: 900px; height: 30px; float: left;">
        <input type="text" name="txtAcademicoAsignacionCatedraticos_FECHA-ASIGNACION" value="<?php print $strFechaAsignacion; ?>" style="text-align: center;" maxlength="10">
    </div>
    <div style="width: 100px; height: 30px; float: left;"><b>Fecha Expiración</b></div>
    <div style="width: 900px; height: 30px; float: left;">
        <input type="text" name="txtAcademicoAsignacionCatedraticos_FECHA-EXPIRACION" value="<?php print $strFechaExpiracion; ?>" style="text-align: center;" maxlength="10">
    </div>
    <div style="width: 900px; height: 30px; float: left;">&nbsp;</div>
    <script type="text/javascript">
    $(function(){
        $("input[name='txtAcademicoAsignacionCatedraticos_FECHA-ASIGNACION']").datepicker({
            changeMonth: true,
            changeYear: true,
            monthNamesShort: ['Ene','Feb','Mar','Abr','May','Jun','Jul','Ago','Sep','Oct','Nov','Dic'],
            dayNamesMin: ['Do', 'Lu', 'Ma', 'Mi', 'Ju', 'Vi', 'Sa'],
            dateFormat: 'dd-mm-yy'
        });
        $("input[name='txtAcademicoAsignacionCatedraticos_FECHA-EXPIRACION']").datepicker({
            changeMonth: true,
            changeYear: true,
            monthNamesShort: ['Ene','Feb','Mar','Abr','May','Jun','Jul','Ago','Sep','Oct','Nov','Dic'],
            dayNamesMin: ['Do', 'Lu', 'Ma', 'Mi', 'Ju', 'Vi', 'Sa'],
            dateFormat: 'dd-mm-yy'
        });
        $("[name='txtAcademicoAsignacionCatedraticos_fk-grado']").change( function () {
            fntAcademicoAsignacionCatedraticosContenidoSeccion();
            fntAcademicoAsignacionCatedraticosContenidoMateria();
        });    
    });
    </script>
    <?php                                                                         
    
}
function fntAcademicoAsignacionCatedraticosContenidoSecciones($db) {

    //VARIBLES
    $strId = "";
    $strQuery = "";
    //VARIBLES
    $strGrado = isset($_POST["txtAcademicoAsignacionCatedraticos_fk-grado"]) ? $_POST["txtAcademicoAsignacionCatedraticos_fk-grado"] : "";
    ?>
    <select name="txtAcademicoAsignacionCatedraticos_fk-seccion">
        <option value="0"></option>
    <?php
    $strQuery ="SELECT  * 
                FROM    cllg_academico_secciones,
                        cllg_academico_grados_secciones 
                WHERE   cllg_academico_secciones.ID = cllg_academico_grados_secciones.FK_SECCION
                AND     cllg_academico_grados_secciones.FK_GRADO = '{$strGrado}'";
    $qTMP = $db->query($strQuery);
    while ( $rTMP = $db->fetch_array($qTMP) ) { 
        $strSelected = ( $strGrado==$rTMP["ID"] ) ? "selected" : "";
        ?>
        <option value="<?php print $rTMP["ID"]; ?>"><?php print $rTMP["NOMBRE"]; ?></option>
        <?php        
    }
    $db->free_result($qTMP);
    ?>
    </select>
    <script type="text/javascript">
    $("[name='txtAcademicoAsignacionCatedraticos_fk-seccion']").change( function () {
        fntAcademicoAsignacionCatedraticosContenidoMateria();
    });
    </script>
    <?php
    
}
function fntAcademicoAsignacionCatedraticosContenidoMaterias($db){

    //VARIBLES
    $strQuery = "";
    $arrAsignados = array();
    //VARIBLES
    $strPeriodo = isset($_POST["txtAcademicoAsignacionCatedraticos_fk-periodo"]) ? $_POST["txtAcademicoAsignacionCatedraticos_fk-periodo"] : "";
    $strGrado = isset($_POST["txtAcademicoAsignacionCatedraticos_fk-grado"]) ? $_POST["txtAcademicoAsignacionCatedraticos_fk-grado"] : "";
    $strSeccion = isset($_POST["txtAcademicoAsignacionCatedraticos_fk-seccion"]) ? $_POST["txtAcademicoAsignacionCatedraticos_fk-seccion"] : "";
    
    $strQuery ="SELECT  * 
                FROM    cllg_academico_catedraticos_asignacion 
                WHERE   cllg_academico_catedraticos_asignacion.FK_PERIODO  = '{$strPeriodo}'
                AND     cllg_academico_catedraticos_asignacion.FK_GRADO = '{$strGrado}'
                AND     cllg_academico_catedraticos_asignacion.FK_SECCION = '{$strSeccion}'";
    $qTMP = $db->query($strQuery);
    while ( $rTMP = $db->fetch_array($qTMP) ) { 
        $arrAsignados[$rTMP["FK_MATERIA"]] = $rTMP["FK_MATERIA"]; 
    }
    $db->free_result($qTMP);
         /*drawDebug($strQuery);
         drawDebug($arrAsignados);*/
    $strQuery ="SELECT  cllg_academico_materias.*      
                FROM    cllg_academico_pensum,
                        cllg_academico_pensum_detail,
                        cllg_academico_materias 
                WHERE   cllg_academico_pensum.ID = cllg_academico_pensum_detail.FK_PENSUM
                AND     cllg_academico_pensum.FK_PERIODO  = '{$strPeriodo}'
                AND     cllg_academico_pensum.FK_GRADO = '{$strGrado}'
                AND     cllg_academico_pensum_detail.FK_MATERIA = cllg_academico_materias.ID ";
    $qTMP = $db->query($strQuery);
    ?>
    <select name="txtAcademicoAsignacionCatedraticos_FK-MATERIA">
    <?php
    while ( $rTMP = $db->fetch_array($qTMP) ) {
        if( !isset($arrAsignados[$rTMP["ID"]]) ) { 
            $strSelected = ( $strPeriodo==$rTMP["ID"] ) ? "selected" : "";
            ?>
            <option value="<?php print $rTMP["ID"]; ?>"><?php print $rTMP["NOMBRE"]; ?></option>
            <?php
        }
    }
    $db->free_result($qTMP);
    ?>
    </select>
    <?php
    
}
function fntAcademicoAsignacionCatedraticosReporte($db){

    //VARIBLES
    $strId = "";
    $strQuery = "";
    //VARIBLES
    $strCatedratico = isset($_POST["txtAcademicoAsignacionCatedraticos_FK-CATEDRATICO"]) ? $_POST["txtAcademicoAsignacionCatedraticos_FK-CATEDRATICO"] : "";
    $strQuery = "SELECT CA.ID, CA.FK_CATEDRATICO, CA.FK_PERIODO, CA.FK_GRADO, 
                        DATE_FORMAT(CA.FECHA_ASIGNACION, '%d-%m-%Y') AS FECHA_ASIGNACION, 
                        DATE_FORMAT(CA.FECHA_EXPIRACION, '%d-%m-%Y') AS FECHA_EXPIRACION , 
                        T.FIRST_NAME, T.LAST_NAME,  
                        P.NOMBRE AS PERIODO,
                        G.NOMBRE AS GRADO,
                        S.NOMBRE AS SECCION,
                        M.NOMBRE AS MATERIA
                FROM    cllg_academico_catedraticos_asignacion AS CA,
                        cllg_teacher AS T,
                        cllg_academico_periodos AS P,
                        cllg_academico_grados AS G,
                        cllg_academico_secciones AS S,
                        cllg_academico_materias AS M 
                WHERE   CA.FK_CATEDRATICO = T.ID
                AND     CA.FK_PERIODO = P.ID
                AND     CA.FK_GRADO = G.ID
                AND     CA.FK_SECCION = S.ID
                AND     CA.FK_MATERIA = M.ID
                AND     T.ID = '{$strCatedratico}'
                ORDER   BY CA.FK_PERIODO, G.NOMBRE, S.NOMBRE, M.NOMBRE ";
    $qTMP = $db->query($strQuery);
    $intNumRows = $db->num_rows;
    
    ?>
    <style type="">
    table {
        border-collapse: collapse;
    }
    table td{
        border: 1px solid #AAAAAA;        
    }
    .oddtr {
        background-color:#FFFFFF;
    }
    .eventr {
        background-color:#FFFFFF;
    }
    .trover {
        background-color: #ECEAE0;
    }
    .trclick {
        background-color: #ECEAE0;
    }
    </style>
    <br>
    <br>
    <table width="100%" cellpadding="2" cellspacing="0" border="0">
        <tbody>
            <td width="15%" align="center" style="background-color: #ECEAE0;"><b>Periodo</b></td>
            <td width="15%" align="center" style="background-color: #ECEAE0;"><b>Grado</b></td>
            <td width="15%" align="center" style="background-color: #ECEAE0;"><b>Seccion</b></td>
            <td width="25%" align="center" style="background-color: #ECEAE0;"><b>Asignatura</b></td>
            <td width="15%" align="center" style="background-color: #ECEAE0;"><b>Fecha Asignación</b></td>
            <td width="15%" align="center" style="background-color: #ECEAE0;"><b>Fecha Expiración</b></td>
        </tbody>
    </table>
    <table width="100%" cellpadding="2" cellspacing="0" border="0" id="tblAcademicoAsignacionCatedraticos">   
        <?php    
        if( $intNumRows ) {
        
            while ( $rTMP = $db->fetch_array($qTMP) ) { 
                ?>
                <tr id="<?php print $rTMP["ID"]; ?>">
                    <td width="15%" align="center"><?php print $rTMP["PERIODO"]; ?></td>
                    <td width="15%" align="center"><?php print $rTMP["GRADO"]; ?></td>
                    <td width="15%" align="center"><?php print $rTMP["SECCION"]; ?></td>
                    <td width="25%" align="center"><?php print $rTMP["MATERIA"]; ?></td>
                    <td width="15%" align="center"><?php print $rTMP["FECHA_ASIGNACION"]; ?></td>
                    <td width="15%" align="center"><?php print $rTMP["FECHA_EXPIRACION"]; ?></td>
                </tr>        
                <?php
            }
            
        }
        $db->free_result($qTMP);
        ?>
    </table>
    <script type="">
    //these two line adds the color to each different row
    $("#tblAcademicoAsignacionCatedraticos tr:even").addClass("eventr");;
    $("#tblAcademicoAsignacionCatedraticos tr:odd").addClass("oddtr");;
    //handle the mouseover , mouseout and click event
    $("#tblAcademicoAsignacionCatedraticos tr").mouseover(function() {
        $(this).addClass("trover");
    }).mouseout(function() {
        $(this).removeClass("trover");
    }).click(function() {
        //$("tr[id!='"+$(this).attr("id")+"']").removeClass("trover");
        //$(this).addClass("trover");        
        //$(this).toggleClass("trclick");
        $("input[name='hdnAcademicoAsignacionCatedraticos_Id']").val( $(this).attr("id") );
        fntAcademicoAsignacionCatedraticosContenido();
    });
    </script>
    <?php
    
}
function fntAcademicoAsignacionCatedraticosGrabar($db){

    $strId = isset($_POST["hdnAcademicoAsignacionCatedraticos_Id"]) ? $_POST["hdnAcademicoAsignacionCatedraticos_Id"] : "";
    $strFields = "";
    $strValues = "";
    
    while( $arrPost = each($_POST) )  {
        $arrExplode = explode( "_", $arrPost["key"]);
        
        if( $arrExplode[0] == "txtAcademicoAsignacionCatedraticos" ) {
            if( $arrPost["key"] != "hdnAcademicoAsignacionCatedraticos_Id" ){
                
                if( $arrExplode[1] == "FECHA-ASIGNACION" || $arrExplode[1] == "FECHA-EXPIRACION" ) {
                    $arrTMP = array();
                    $arrTMP = explode( "-", $arrPost["value"] );
                    krsort($arrTMP);
                    $arrPost["value"] = implode("-", $arrTMP);
                }
                
                if( empty($strId) ) {
                    $strFields .= (empty($strFields)) ? "" : ","; 
                    $strFields .=  strtoupper( str_ireplace( "-", "_", $arrExplode[1] ) );
                    
                    $strValues .= empty( $strValues ) ? "" : ",";
                    $strValues .= "'{$arrPost["value"]}'";
                }
                else {
                    $strFields .= (empty($strFields)) ? "" : ",";
                    $strFields .=  strtoupper( str_ireplace( "-", "_", $arrExplode[1] ) ). " = '{$arrPost["value"]}'"; 
                    
                }
            }
        }
        
    }
    if( empty($strId) ) {
        $db->query("INSERT INTO cllg_academico_catedraticos_asignacion ({$strFields}) VALUES ({$strValues})");
        /*$intID = mysql_insert_id ( $db->link_id ); 
        reset($_POST);
        while( $arrPost1 = each($_POST) ) {
            $arrExplode = explode( "_", $arrPost1["key"]);
            if( $arrExplode[0] == "hdnAcademicoAsignacionCatedraticosD" && $arrExplode[1] == "DESCRIPCION" ) {
                $strDescripcion = isset($_POST["hdnAcademicoAsignacionCatedraticosD_DESCRIPCION_{$arrExplode[2]}"]) ? $_POST["hdnAcademicoAsignacionCatedraticosD_DESCRIPCION_{$arrExplode[2]}"] : "";
                $strValor = isset($_POST["hdnAcademicoAsignacionCatedraticosD_VALOR_{$arrExplode[2]}"]) ? $_POST["hdnAcademicoAsignacionCatedraticosD_VALOR_{$arrExplode[2]}"] : "";
                $strVigencia = isset($_POST["hdnAcademicoAsignacionCatedraticosD_VIGENCIA_{$arrExplode[2]}"]) ? $_POST["hdnAcademicoAsignacionCatedraticosD_VIGENCIA_{$arrExplode[2]}"] : "";
                $strCTACobrar = isset($_POST["hdnAcademicoAsignacionCatedraticosD_CTA-COBRAR_{$arrExplode[2]}"]) ? $_POST["hdnAcademicoAsignacionCatedraticosD_CTA-COBRAR_{$arrExplode[2]}"] : "";
                $strCTAIngresos = isset($_POST["hdnAcademicoAsignacionCatedraticosD_CTA-INGRESOS_{$arrExplode[2]}"]) ? $_POST["hdnAcademicoAsignacionCatedraticosD_CTA-INGRESOS_{$arrExplode[2]}"] : "";
                $strTipo = isset($_POST["hdnAcademicoAsignacionCatedraticosD_TIPO_{$arrExplode[2]}"]) ? $_POST["hdnAcademicoAsignacionCatedraticosD_TIPO_{$arrExplode[2]}"] : "";
                
                $db->query("INSERT INTO cllg_academico_cargos_detail (FK_CARGOS, DESCRIPCION, VALOR, VIGENCIA, CTA_COBRAR, CTA_INGRESOS, TIPO) 
                            VALUES                                   ('{$strId}', '{$strDescripcion}', '{$strValor}', '{$strVigencia}', '{$strCTACobrar}', '{$strCTAIngresos}', '{$strTipo}' )");
            }                        
        }*/
    }
    else {
        $db->query("UPDATE cllg_academico_catedraticos_asignacion SET {$strFields} WHERE ID = '{$strId}'");
        /*$db->query("DELETE FROM cllg_academico_cargos_detail WHERE FK_CARGOS = '{$strId}'");
        reset($_POST);
        while( $arrPost1 = each($_POST) ) {
            $arrExplode = explode( "_", $arrPost1["key"]);
            if( $arrExplode[0] == "hdnAcademicoAsignacionCatedraticosD" && $arrExplode[1] == "DESCRIPCION" ) {
                $strDescripcion = isset($_POST["hdnAcademicoAsignacionCatedraticosD_DESCRIPCION_{$arrExplode[2]}"]) ? $_POST["hdnAcademicoAsignacionCatedraticosD_DESCRIPCION_{$arrExplode[2]}"] : "";
                $strValor = isset($_POST["hdnAcademicoAsignacionCatedraticosD_VALOR_{$arrExplode[2]}"]) ? $_POST["hdnAcademicoAsignacionCatedraticosD_VALOR_{$arrExplode[2]}"] : "";
                $strVigencia = isset($_POST["hdnAcademicoAsignacionCatedraticosD_VIGENCIA_{$arrExplode[2]}"]) ? $_POST["hdnAcademicoAsignacionCatedraticosD_VIGENCIA_{$arrExplode[2]}"] : "";
                $strCTACobrar = isset($_POST["hdnAcademicoAsignacionCatedraticosD_CTA-COBRAR_{$arrExplode[2]}"]) ? $_POST["hdnAcademicoAsignacionCatedraticosD_CTA-COBRAR_{$arrExplode[2]}"] : "";
                $strCTAIngresos = isset($_POST["hdnAcademicoAsignacionCatedraticosD_CTA-INGRESOS_{$arrExplode[2]}"]) ? $_POST["hdnAcademicoAsignacionCatedraticosD_CTA-INGRESOS_{$arrExplode[2]}"] : "";
                $strTipo = isset($_POST["hdnAcademicoAsignacionCatedraticosD_TIPO_{$arrExplode[2]}"]) ? $_POST["hdnAcademicoAsignacionCatedraticosD_TIPO_{$arrExplode[2]}"] : "";
                
                $db->query("INSERT INTO cllg_academico_cargos_detail (FK_CARGOS, DESCRIPCION, VALOR, VIGENCIA, CTA_COBRAR, CTA_INGRESOS, TIPO) 
                            VALUES                                   ('{$strId}', '{$strDescripcion}', '{$strValor}', '{$strVigencia}', '{$strCTACobrar}', '{$strCTAIngresos}', '{$strTipo}' )");
            }                        
        }*/
    }
    
    
}
function fntAcademicoAsignacionCatedraticosContabilidadGetCuentasUltimoNivel($db) {
    
    $arrResult = array();
    //$empresa = getParam("empresa");
    $empresa = 1;
    $term = getParam("term");
    $strQuery = "SELECT c1.* FROM cllg_contab_cuenta c1
                 LEFT JOIN cllg_contab_cuenta c2 ON(c1.codigo = c2.fk_padre AND c1.fk_empresa = c2.fk_empresa )
                 WHERE (c1.codigo LIKE '%{$term}%' OR c1.nombre LIKE '%{$term}%') 
                 AND    c1.fk_empresa = '{$empresa}'
                 AND    c2.codigo IS NULL";
    $qTMP = $db->query($strQuery);
        
    if( $rTMP = $db->fetch_array($qTMP) ) {

        do {
            $arrTMP =  array();
            $arrTMP["id"] = utf8_encode($rTMP["codigo"]);
            $arrTMP["value"] = utf8_encode($rTMP["codigo"]." - ".$rTMP["nombre"]);
            
            array_push($arrResult,$arrTMP);
            
        } while ($rTMP = $db->fetch_array($qTMP));
        
    }
    $db->free_result($rTMP);
    
    print json_encode($arrResult);    
    
}
?>